package com.tumblr.logico2012.screens;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

import com.tumblr.logico2012.gestures.GestureManager;
import com.tumblr.logico2012.gestures.IGesture;
import com.tumblr.logico2012.gestures.ISwipeGesture;

import static com.tumblr.logico2012.AppletLogico.DISABLE_CAMERA;

/**
 * Game screen. The interface IScreen is not completely implemented: the
 * method leave is implemented in AppletLogico.ShowGameScreen.
 * 
 * The return value is "yes" if the game should be started, "no" is the previous screen
 * should be shown again
 */
public abstract class IShowGameScreen implements IScreen {
	protected PApplet p;
	protected PImage m_img;
	protected String m_title;
	protected boolean m_play;

	public final static float DELAY = 2f;
	
	// COLOURS
	int clBackground;
	int clForeground;

	// FONTS
	PFont fHeader;

	int m_framecounter;
	
	public IShowGameScreen(PApplet processor, GestureManager manager, PImage img, String title) {
		m_img = img;
		m_title = title;
		p = processor;
		m_play = false;

		clBackground = p.color(230,220,210);
		clForeground = p.color(30,20,10);
		fHeader = p.createFont("Georgia", 50);
		
		manager.setGestureInstruction("swipeRightOut", null);
		manager.setGestureInstruction("swipeLeftOut", "Quit");
		m_framecounter = 0;
	}
	
	@Override
	public void registerUser(GestureManager manager, int userId) {	
	    manager.addGesture(new SwipeGesture(userId, IGesture.RIGHT, IGesture.RIGHT));
//	    manager.addGesture(new SwipeGesture(userId, IGesture.LEFT, IGesture.RIGHT));
//	    manager.addGesture(new SwipeGesture(userId, IGesture.RIGHT, IGesture.LEFT));
	    manager.addGesture(new SwipeGesture(userId, IGesture.LEFT, IGesture.LEFT));	
	}
	
	@Override
	public void keyPressed() {
		if(DISABLE_CAMERA) {
			switch(p.key) {
			case PApplet.ESC:
				p.key = 0;
				leave();
				break;
			}
		}
	}
	
	@Override
	public void draw() {
		p.background(clBackground);

		p.fill(clForeground); // game image
		p.textFont(fHeader);
		p.stroke(clForeground); p.strokeWeight(5);
		p.rect(p.width/2 - 225, p.height/2 - 200, 450, 300);
		p.image(m_img, p.width/2 - 225, p.height/2 - 200, 450, 300);
		p.text(m_title, p.width/2 - p.textWidth(m_title)/2, p.height/2 + 150);

		m_framecounter ++;
	}

	@Override
	public String returnValue() {
		return m_play ? "yes" : "no"; // "yes": play, "no": go back
	}

	class SwipeGesture extends ISwipeGesture {
		public SwipeGesture(int userId, int hand, int direction) {
			super(userId, hand, direction, IShowGameScreen.this.p, direction == IGesture.LEFT ? "swipeLeftOut" : "swipeRightOut");
		}

		@Override
		public void gestureAborted(GestureManager manager) {
			PApplet.println("swipe aborted!");
		}

		@Override
		public void gestureEnded(GestureManager manager) {
			PApplet.println("swipe ended!");
			if(m_framecounter > p.frameRate*DELAY) // small delay before being able to leave,
				// to avoid false positives when the user takes back his/her arm.
				leave();
		}

		@Override
		public void gestureStarted(GestureManager manager) {
			PApplet.println("swipe started!");
		}
	}	
}
